home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / freeware / openvip.exe / {app} / Lib / encodings / __init__.py next >
Encoding:
Python Source  |  2002-08-18  |  2.9 KB  |  98 lines

  1. """ Standard "encodings" Package
  2.  
  3.     Standard Python encoding modules are stored in this package
  4.     directory.
  5.  
  6.     Codec modules must have names corresponding to standard lower-case
  7.     encoding names with hyphens mapped to underscores, e.g. 'utf-8' is
  8.     implemented by the module 'utf_8.py'.
  9.  
  10.     Each codec module must export the following interface:
  11.  
  12.     * getregentry() -> (encoder, decoder, stream_reader, stream_writer)
  13.     The getregentry() API must return callable objects which adhere to
  14.     the Python Codec Interface Standard.
  15.  
  16.     In addition, a module may optionally also define the following
  17.     APIs which are then used by the package's codec search function:
  18.  
  19.     * getaliases() -> sequence of encoding name strings to use as aliases
  20.  
  21.     Alias names returned by getaliases() must be standard encoding
  22.     names as defined above (lower-case, hyphens converted to
  23.     underscores).
  24.  
  25. Written by Marc-Andre Lemburg (mal@lemburg.com).
  26.  
  27. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  28.  
  29. """#"
  30.  
  31. import codecs,aliases,exceptions
  32.  
  33. _cache = {}
  34. _unknown = '--unknown--'
  35.  
  36. class CodecRegistryError(exceptions.LookupError,
  37.                          exceptions.SystemError):
  38.     pass
  39.  
  40. def search_function(encoding):
  41.     
  42.     # Cache lookup
  43.     entry = _cache.get(encoding,_unknown)
  44.     if entry is not _unknown:
  45.         return entry
  46.  
  47.     # Import the module
  48.     modname = encoding.replace('-', '_')
  49.     modname = aliases.aliases.get(modname,modname)
  50.     try:
  51.         mod = __import__(modname,globals(),locals(),'*')
  52.     except ImportError,why:
  53.         # cache misses
  54.         _cache[encoding] = None
  55.         return None
  56.  
  57.     try:
  58.         getregentry = mod.getregentry
  59.     except AttributeError:
  60.         # Not a codec module
  61.         _cache[encoding] = None
  62.         return None
  63.     
  64.     # Now ask the module for the registry entry
  65.     try:
  66.         entry = tuple(getregentry())
  67.     except AttributeError:
  68.         entry = ()
  69.     if len(entry) != 4:
  70.         raise CodecRegistryError,\
  71.               'module "%s" (%s) failed to register' % \
  72.               (mod.__name__, mod.__file__)
  73.     for obj in entry:
  74.         if not callable(obj):
  75.             raise CodecRegistryError,\
  76.                   'incompatible codecs in module "%s" (%s)' % \
  77.                   (mod.__name__, mod.__file__)
  78.  
  79.     # Cache the codec registry entry
  80.     _cache[encoding] = entry
  81.  
  82.     # Register its aliases (without overwriting previously registered
  83.     # aliases)
  84.     try:
  85.         codecaliases = mod.getaliases()
  86.     except AttributeError:
  87.         pass
  88.     else:
  89.         for alias in codecaliases:
  90.             if not aliases.aliases.has_key(alias):
  91.                 aliases.aliases[alias] = modname
  92.  
  93.     # Return the registry entry
  94.     return entry
  95.  
  96. # Register the search_function in the Python codec registry
  97. codecs.register(search_function)
  98.